package com.sdkj.action.service;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sdkj.Entity.service.Advertise;
import com.sdkj.action.base.BaseAction;
import com.sdkj.service.service.IAdvertiseService;
import com.sdkj.utils.CommonUtils;
import com.sdkj.utils.PageBean;

@Controller
@Scope("prototype")
public class AdvertiseAction extends BaseAction<Advertise, IAdvertiseService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public void save() throws SQLException{
		Serializable s=service.save(model);
		JSONObject res=new JSONObject(); 
		if(s!=null){  
			res.put(RES_CODE, 100); 
			res.put(RES_MSG, "保存成功"); 
			res.put("id", s);
			try {
				operationLogService.save(getOperationLogBean("save ads",new String[]{"askKills","work","ext"}));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "保存失败");
		}
		CommonUtils.writeJson(res);
	}

	public void list() throws SQLException{  
		String hql=" from Advertise order by createTime desc";
		
		Map<String,Object>params=new HashMap<String,Object>();
		PageBean pb=new PageBean(page, service.count("select count(*) "+hql,params), rows);
		List<Advertise>data=service.find(hql, params, page, rows);
		JSONObject res=new JSONObject();
		res.put(RES_CODE, 100);
		res.put(RES_MSG, "查询成功");
		res.put("pagebean", pb);
		res.put("data", JSONArray.fromObject(data));
		CommonUtils.writeJson(res);
	}
	
	public void update() throws SQLException{
		Serializable s=service.update(model);
		JSONObject res=new JSONObject(); 
		if(s!=null){  
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "修改成功");
			res.put("id", s);
		}else{
			res.put(RES_CODE, -100);
			res.put(RES_MSG, "修改失败"); 
		}
		CommonUtils.writeJson(res);
	}
	public void del() throws SQLException{
		boolean flag=service.deleteIds(ids);
		JSONObject res=new JSONObject();
		if(flag){
			res.put(RES_CODE, 100);
			res.put(RES_MSG, "删除数据成功");
		}else{
			res.put(RES_CODE, -100); 
			res.put(RES_MSG, "删除数据失败");
		}
		try {
			operationLogService.save(getOperationLogBean("delete ads",new String[]{"askKills","work","ext"}));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		CommonUtils.writeJson(res);
	}

}
